﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace TomVanGaever.BCS.Parser
{
    public partial class BCSParserEntityIdentifierPickerControl : BCSUserControl
    {

        public IEnumerable<PropertyInfo> DataSource { get; set; }
        public Type SelectedEntityType { get; set; }
        public PropertyInfo SelectedEntityProperty { get { return lstIdentifierProperties.SelectedItem as PropertyInfo; } }
        public string TypeMember { get; set; }

        private Func<Type> GetSelectedType;

		public override string TitleText
		{
			get
			{
				label2.Visible = false;
				return label2.Text;
			}
		} 


        public void GetSelectedEntityType(Func<Type> action)
        {
            this.GetSelectedType = action;
        }

        public void DataBind()
        {
            lstIdentifierProperties.DisplayMember = this.TypeMember;
            lstIdentifierProperties.DataSource = DataSource;
        }

        public BCSParserEntityIdentifierPickerControl()
        {
            InitializeComponent();
        }

        public override string GetStatusString()
        {
            string result = "Selected identifier this entity: \t\t\t\t {0}";
            return string.Format(result, this.SelectedEntityProperty.Name);
        }

        private void BCSParserEntityIdentifierPickerControl_Load(object sender, EventArgs e)
        {
            this.SelectedEntityType = this.GetSelectedType.Invoke();
            this.TypeMember = TypeMember;
            this.DataSource = this.SelectedEntityType.GetProperties().ToList();
            this.DataBind();
        }

    }
}
